package m.fun.action;

import m.base.action.BaseStatusAction;
import m.base.model.AppInfo;
import m.base.model.LevelInfo;
import m.base.util.LevelUtil;
import m.base.util.PowerUtil;
import m.common.action.*;
import m.common.model.util.ModelQueryList;
import m.common.model.util.ModelUpdateUtil;
import m.common.model.util.QueryCondition;
import m.fun.model.OfferInfo;
import m.fun.service.OfferInfoService;
import m.html.VueFormResult;
import m.html.VueListResult;
import m.html.element.Element;
import m.html.element.attr.Align;
import m.html.element.attr.ButtonType;
import m.html.form.*;
import m.html.list.EventType;
import m.html.list.ListButton;
import m.html.list.ListEvent;
import m.html.list.SuccessMethod;
import m.system.cache.CacheUtil;
import m.system.exception.MException;
import m.system.lang.HtmlBodyContent;
import m.system.util.JSONMessage;
import m.system.util.StringUtil;

@ActionMeta(name = "fun/offerInfo")
public class OfferInfoAction extends BaseStatusAction {
    private OfferInfo model;
    private void verifyFunc() throws Exception {
        if(!LevelUtil.offerFunc()){
            throw new MException(this.getClass(),"当前商户未开通报价单功能")
            .setRedirect(LevelUtil.toErrorPage("当前商户未开通报价单功能",false));
        }
    }
    private OfferInfo getViewModel(String oid) throws Exception {
        model=ModelQueryList.getModel(OfferInfo.class,oid,"*","store.level.offerFunc");
        if(LevelInfo.No.equals(model.getStore().getLevel().getOfferFunc())){
            throw new MException(this.getClass(),"当前商户未开通报价单功能");
        }
        model.setViewNum(model.getViewNum()+1);
        ModelUpdateUtil.updateModel(model,"viewNum");
        return model;
    }
    private JSONMessage getViewAppInfo(){
        AppInfo app= CacheUtil.get(AppInfo.class,"");
        JSONMessage appInfo=new JSONMessage();
        appInfo.push("offerBottom1Txt",app.getOfferBottom1Txt());
        appInfo.push("offerBottom2Txt",app.getOfferBottom2Txt());
        appInfo.push("offerBottom2Link",app.getOfferBottom2Link());
        return appInfo;
    }
    @RequestMeta(params = {"oid"})
    public JSONMessage viewApp(String oid) throws Exception {
        return JSONMessage.success("查询成功")
                .push("model",getViewModel(oid))
                .push("appInfo",getViewAppInfo());
    }
    @RequestMeta(params = {"oid"})
    public HtmlResult viewH5(String oid) throws Exception {
        return new HtmlPageResult("page/fun/offerInfo.html")
                .addParam("model",getViewModel(oid))
                .addParam("appInfo",getViewAppInfo());
    }
    @RequestMeta(logTitle = "报价单",logDescription = "保存报价单信息")
    public JSONMessage doSave() throws Exception {
        model.setUser(PowerUtil.getSessionUser(true));
        String msg=getService(OfferInfoService.class).save(model);
        return new JSONMessage(0,msg);
    }
    /*
     * 编辑报价单
     */
    public VueResult toEdit() throws Exception {
        if(null!=model&&!StringUtil.isSpace(model.getOid())){
            model= ModelQueryList.getModel(model,"*");
        }else{
            model=new OfferInfo();
            model.setTitle("报价单");
            model.setDescription("");
            model.setUser(PowerUtil.getSessionUser(true));
            model.setStore(PowerUtil.getSessionStore(true));
            model.setContact(model.getUser().getRealname());
            model.setTel(model.getUser().getPhone());
            model.setAddress(PowerUtil.getSessionStore(true).getAddress());
        }
        ModelForm form = new ModelForm("报价单",2);
        form.addItem(
            FormInput.input("标题", "model.title").setRequired(true).setSpan(2).setPlaceholder("请输入标题"),
            FormInput.textarea("描述", "model.description").setSpan(2).setPlaceholder("请输入描述"),
            FormInput.input("联系人", "model.contact").setRequired(true).setPlaceholder("请输入联系人"),
            FormInput.input("联系电话", "model.tel").setRequired(true).setPlaceholder("请输入联系电话"),
            FormInput.input("地址", "model.address").setRequired(true).setSpan(2).setPlaceholder("请输入地址")
        )
        .addData("model",model)
        .addButton(
            FormButton.pri("保存","doSave")
        )
        .addMethod(
            FormEvent.backFlag,
            FormEvent.doJson("doSave",actionUrl("doSave"),"{model:this.model}")
                .success("backFlag")
        );
        VueFormResult result=new VueFormResult();
        return result.addForm(form);
    }
    public VueResult toView() throws Exception {
        if(null!=model&&!StringUtil.isSpace(model.getOid())){
            model= ModelQueryList.getModel(model,"*");
        }else{
            throw new MException(this.getClass(),"参数错误");
        }
        CustomForm form=new CustomForm("报价单");
        form.addElement(
            Element.tag("div").A("style","width:390px;").E(
                Element.tag("a-typography-title").A("class","padding-lr padding-top").A(":level","5").E("{{model.title}}"),
                Element.tag("a-typography-paragraph").A("class","padding-lr").A("v-for","t in model.description.split('\\\\n')").E("{{t}}").A("style","text-indent: 28px;"),
                Element.tag("a-typography-paragraph").A("class","padding-lr").E("{{model.contact}} {{model.tel}}<br>{{model.address}}"),
                Element.tag("div").A("class","text-center padding_top").E(
                    Element.tag("div").E("{{hostUrl+'/OFFER'+model.oid}}").A("class","padding-top"),
                    Element.tag("img").A(":src","'qrcode?txt='+encodeURIComponent(hostUrl+'/OFFER'+model.oid)").A("style","width:300px;")
                )
            )
        )
        .addData("hostUrl",new HtmlBodyContent("location.origin"))
        .addData("model",model);
        VueFormResult result=new VueFormResult();
        return result.addForm(form)
            .setLeftForm(new CustomForm(
                Element.tag("iframe").A("width","100%").A("height","670px")
                    .A("frameborder","0").A("class","solid line-gray")
                    .A(":src","'/OFFER'+model.oid")
            ),425);
    }
    /*
     * 报价单列表
     */
    public VueResult myList() throws Exception {
        verifyFunc();
        VueListResult result=getVueListResult("我的报价单",OfferInfo.class,actionUrl("myList"));
        result.setSubTitle("点击标题可以查看效果和访问地址。");
        result.setFunction(LevelUtil.exportFunc(),false);
        result.setHeight(-1);
        result.addCol(toIndexColumn());
        result.addCol(toLinkColumn("标题", "title", 150,
            new ListEvent(actionUrl("toView"), EventType.MODAL,850)
                .addParam("model.oid","oid")
        ));
        result.addCol(toTextColumn("描述", "description", 300, true));
        result.addCol(toTextColumn("联系人", "contact", 100, true));
        result.addCol(toTextColumn("联系电话", "tel", 100, true));
        result.addCol(toTextColumn("地址", "address", 130, true));
        result.addCol(toIntegerColumn("报价数量", "detailNum", 100, true));
        result.addCol(toIntegerColumn("访问量", "viewNum", 100, true));
        result.addCol(toStatusColumn(true, true).setDictType("m.fun.model.OfferInfo!status"));
        result.addCol(toSpaceColumn("操作", 140,
            ListButton.modal("修改", ButtonType.PRIMARY,actionUrl("toEdit"),700, SuccessMethod.REFRESH)
                .addParam("model.oid","oid"),
            ListButton.modal("报价明细", ButtonType.GHOST,actionUrl(OfferDetailAction.class,"detailList"),1300, SuccessMethod.MUST_REFRESH)
                .addParam("params[offer.oid]","oid")
        ).setFixed(Align.RIGHT));
        result.addButton(
            ListButton.modal("新增", ButtonType.PRIMARY,actionUrl("toEdit"),700, SuccessMethod.REFRESH)
        );
        //排序 result.addOrder
        //查询条件 result.addCondition
        result.addCondition(QueryCondition.eq("user.oid", PowerUtil.getSessionUserOid()));
        return result;
    }
    @Override
    public OfferInfo getModel() {
        return model;
    }

    @Override
    public String[] getStatusPower() {
        return new String[0];
    }

    public void setModel(OfferInfo model) {
        this.model = model;
    }
}
